package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.module.admin.service.ISysDictDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysDoctorRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysDoctorMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysDoctor;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysDoctorService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 医生业务层处理
 *
 * @author mars
 * @date 2024-04-11
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDoctorServiceImpl implements ISysDoctorService {

    private final SysDoctorMapper baseMapper;
    private final ISysDictDataService dataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDoctor add(SysDoctorRequest request) {
        SysDoctor entity = SysDoctor.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysDoctorRequest request) {
        SysDoctor entity = SysDoctor.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysDoctor getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysDoctor> pageList(SysDoctorRequest request) {
        Page<SysDoctor> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysDoctor> query = this.buildWrapper(request);
        IPage<SysDoctor> pageRecord = baseMapper.selectPage(page, query);
        List<SysDoctor> records = pageRecord.getRecords();
        List<SysDoctor> recordsNew = new ArrayList<>();
        for (SysDoctor doctor : records) {
            doctor.setBusinessName(dataService.getDictDataById("business_type", doctor.getBusiness().toString()));
            recordsNew.add(doctor);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysDoctor> list(SysDoctorRequest request) {
        LambdaQueryWrapper<SysDoctor> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysDoctor> buildWrapper(SysDoctorRequest param) {
        LambdaQueryWrapper<SysDoctor> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysDoctor::getName, param.getName());
        }
        if (param.getSex() != null) {
            query.like(SysDoctor::getSex, param.getSex());
        }
        if (StringUtils.isNotBlank(param.getPhone())) {
            query.like(SysDoctor::getPhone, param.getPhone());
        }
        if (param.getAge() != null) {
            query.eq(SysDoctor::getAge, param.getAge());
        }
        return query;
    }

}
